package com.ydl.zhwc.mvpandrxjava.sample.token;

import com.ydl.zhwc.mvpandrxjava.bean.Now;
import com.ydl.zhwc.mvpandrxjava.sample.RxUtils;
import com.ydl.zhwc.mvpandrxjava.sample.http.ApiProvider;
import com.ydl.zhwc.mvpandrxjava.sample.http.ApiService;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2017/12/22
 */

public class TokenModel implements TokenContract.Model {

    @Override
    public Observable<String> getKey() {
        return Observable.just(1)
                .subscribeOn(Schedulers.io())
                .map(new Function<Integer, String>() {
                    @Override
                    public String apply(Integer integer) throws Exception {
                        Thread.sleep(2000);
                        return ApiService.KEY;
                    }
                });
    }

    @Override
    public Observable<Now> getNow(String key) {
        return ApiProvider.getApiService()
                .rxWeather(key, "hangzhou")
                .subscribeOn(Schedulers.io())
                .compose(RxUtils.handlerThrowable());
    }
}
